/**
 *生成服务报告pdf的controller
 */
public with sharing class ServiceReportController {
public WorkOrder__c workOrder{get;set;}
public Id id{get;set;}

public Attachment useSign {get;set;}
public Attachment manageSign {get;set;}
public String useSignDate{get;set;}
public String manageSignDate{get;set;}

public Attachment engineerSign{get;set;}
public String engineerSignDate{get;set;}

    //需要换行的字段
public String reasonDesc{get;set;}
public String note{get;set;}
public String maintenanceInfo{get;set;}
public String techHandle{get;set;}
    public String equipmentModel{get;set;}

public Decimal partsCost{get;set;}
public Decimal totalCost{get;set;}
    
public String equipmentName {set;get;}
public String engineerName {get;set;}//工程师 经销商工单取 服务工程师名字 否则 技术人员
public String serEndTime {get;set;}
public String serStartTime {get;set;}


public List<WorkOrderProduct__c> wpList {get;set;}

    public ServiceReportController(ApexPages.StandardController con) {
    id=con.getId();//工单号
        workOrder = [SELECT 
        w.Account__r.Name,w.Account__r.Address__c,w.Software_Version__c,w.SN__c,
        w.FaultTypes__c,w.TestPerDay__c,w.IMEINumber__c,w.IMEINumber_F__c,w.Postcode__c,
        w.ServiceManufacture__r.Name,w.ServiceManufacture__r.Product__r.Model__c,w.note__c,
        w.ServiceManufacture__r.Product__r.Name,w.OperationCategory__c,w.Technician__r.Name, 
        w.ServiceManufacture__r.Brand__c,  w.NewFaultTypes__c, 
        w.RecordType.DeveloperName,
        w.ChangeNo__c   , 
          
        w.InstallRequestDate__c,w.FaultDeals__c,w.Name,w.Tech_Handle__c,
        w.Install_Start_Datetime__c,Install_End_Datetime__c,w.ReagentConsumables__c,
        w.UserDepartmen__c,w.UserDepartmentContact__r.Name,w.UseDeptTeleNo__c,w.WarrantyStatus__c,
        w.ActuallyWorkTime__c,w.TechExpense__c,w.TravelExpense__c,w.ServiceManufacture__r.EndDate__c,
        w.Case__r.RecordType.DeveloperName,w.Case__r.ServiceEngineerName__c,SampleType__c,
        (SELECT wp.Id,Product__r.Name , Product__r.NCCode__c , wp.Quantity__c,wp.UnitPrice__c,wp.SalesPrice__c,wp.Name,wp.NewPartsNumber__c
        FROM w.WorkOrderuQf3__r wp 
        WHERE WorkOrder__c = :id),
        w.TransportTime__c,w.Maintenance_Info__c,w.Reason_Desc__c,w.Fault_Freq__c,w.ProblemType__c
        FROM WorkOrder__c w 
WHERE w.Id = :id ];

if(null!=workOrder){
wpList = workOrder.WorkOrderuQf3__r;
partsCost = 0;//零件费
for(WorkOrderProduct__c wp:wpList){
partsCost+=(wp.SalesPrice__c==null?0:wp.SalesPrice__c);
}

System.debug(LoggingLevel.INFO, '*** wpList: ' + wpList);
//总费用
totalCost=0;
totalCost = partsCost+(workOrder.TechExpense__c==null?0:workOrder.TechExpense__c)+(workOrder.TravelExpense__c==null?0:workOrder.TravelExpense__c);
System.debug(LoggingLevel.INFO, '*** totalCost: ' + totalCost);

            //不满4个零件要补齐空行
            Integer atLeastRowsCount = 4;
            if(wpList.size()<atLeastRowsCount){
                Integer wpListSize = wpList.size();
                for(Integer i=0;i<(atLeastRowsCount-wpListSize);i++){
                    wpList.add(new WorkOrderProduct__c());
                }
            }

List<Attachment> attachment= [select Body,Name,CreatedDate,Id from Attachment where ParentId = :id];

for(Attachment att : attachment){
if (att.Name=='使用科室负责人_Sign.jpg'){
useSign=att;
}else if( att.Name=='管理科室负责人_Sign.jpg'){
manageSign=att;
}else if( att.Name == '工程师_Sign.jpg'){
engineerSign = att;
}
}

System.debug(LoggingLevel.INFO, '*** attachment: ' + attachment);
System.debug(LoggingLevel.INFO, '*** engineerSign: ' + engineerSign);
    serStartTime = workOrder.Install_Start_Datetime__c != null ? workOrder.Install_Start_Datetime__c.format('yyyy年MM月dd日'):'';
    serEndTime = workOrder.Install_End_Datetime__c != null ? workOrder.Install_End_Datetime__c.format('yyyy年MM月dd日'):'';
    if(workOrder.Install_End_Datetime__c != null )useSignDate = serEndTime;


    if(workOrder.Install_End_Datetime__c != null )engineerSignDate = serEndTime;


engineerName = workOrder.Case__r.RecordType.DeveloperName == 'ServiceOrder' ? workOrder.Case__r.ServiceEngineerName__c : workOrder.Technician__r.Name ;
reasonDesc = Utils.wrapWordInPdf(workOrder.Reason_Desc__c, '\n', 30);
note = Utils.wrapWordInPdf(workOrder.note__c, '\n', 23);
maintenanceInfo = Utils.wrapWordInPdf(workOrder.Maintenance_Info__c, '\n', 30);
techHandle = Utils.wrapWordInPdf(workOrder.Tech_Handle__c, '\n', workOrder.RecordType.DeveloperName =='ServiceOrder' ?  20 : 57);
equipmentModel = Utils.wrapWordInPdf(workOrder.ServiceManufacture__r.Product__r.Model__c, '\n', 9);
   // equipmentModel = Utils.wrapWordInPdf('测试文本测试文本测试测试文本测试文本测试测试文本测试文本测试', '\n', 9);
equipmentName = Utils.wrapWordInPdf(workOrder.ServiceManufacture__r.Brand__c ,'\n',13);
}

    }

}